<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传智健康</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
    <script src="../js/axios-0.18.0.js"></script>
</head>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>当前页面
            <small>一级管理</small>
            <small>二级管理</small>
        </h1>
    </div>
    <div class="app-container">
        <div class="box">
            <div class="filter-container">
                <el-input placeholder="档案号/姓名" v-model="pagination.queryString" style="width: 200px;"
                          class="filter-item"></el-input>
                <el-button @click="this.pagination.currentPage=1;findPage()" class="dalfBut">查询</el-button>
                <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
            </div>
            <el-table @selection-change="handleSelectionChange" size="small" current-row-key="id" :data="dataList"
                      stripe highlight-current-row>
                <el-table-column
                        type="selection"
                        width="55">
                </el-table-column>

                <el-table-column prop="username" align="center" label="用户名"></el-table-column>
                <el-table-column prop="name" label="真实姓名" align="center"></el-table-column>
                <el-table-column label="角色" align="center">
                    <template slot-scope="scope">
                        <span style="margin-left: 2px" v-for="role in scope.row.roles">
                            <span v-text="role.name"></span>
                        </span>
                    </template>
                </el-table-column>
                <el-table-column prop="cont" label="登录次数" align="center" width="80"></el-table-column>
                <el-table-column label="是否启用" align="center" width="80">
                    <template  slot-scope="scope">
                        <span v-text="scope.row.station=='false'?'禁用':'可用'"></span>
                    </template>
                </el-table-column>
                <el-table-column prop="telephone" label="电话号码" align="center"></el-table-column>
                <el-table-column prop="email" label="邮箱" align="center"></el-table-column>
                <el-table-column prop="regitime" label="添加日期" align="center"></el-table-column>
                <el-table-column label="操作" align="center" width="180">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                        <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                        <el-button size="mini" type="danger" @click="handleUpdatePassword(scope.row)">更改密码</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination-container">
                <el-pagination
                        class="pagiantion"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        layout="total, prev, pager, next, jumper"
                        :total="pagination.total">
                </el-pagination>
            </div>

            <!-- 新增标签弹层 -->
            <div class="add-form">
                <el-dialog title="新增用户" :visible.sync="dialogFormVisible">
                    <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right"
                             label-width="100px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="用户名" prop="username">
                                    <el-input v-model="formData.username"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="真实姓名" prop="name">
                                    <el-input v-model="formData.name"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="密码">
                                    <el-input v-model="formData.password" type="password" show-password></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="角色">
                                    <el-select v-model="roleIds" multiple placeholder="请选择">
                                        <el-option
                                                v-for="role in roleList"
                                                :key="role.id"
                                                :label="role.name"
                                                :value="role.id">
                                        </el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>

                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="联系电话">
                                    <el-input v-model="formData.telephone"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="邮箱">
                                    <el-input v-model="formData.email"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="是否禁用">
                                    <el-switch v-model="isForbidden"></el-switch>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                        <el-button type="primary" @click="handleAdd()">确定</el-button>
                    </div>
                </el-dialog>
            </div>

            <!--//编辑-->
            <div class="add-form">
                <el-dialog title="新增用户" :visible.sync="dialogFormVisible4Edit">
                    <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right"
                             label-width="100px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="用户名" prop="username">
                                    <el-input v-model="formData.username"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="真实姓名" prop="name">
                                    <el-input v-model="formData.name"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="密码">
                                    <el-input v-model="formData.password" type="password" show-password></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="角色">
                                    <el-select v-model="roleIds" multiple placeholder="请选择">
                                        <el-option
                                                v-for="role in formData.roles"
                                                :key="role.id"
                                                :label="role.name"
                                                :value="role.id">
                                        </el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>

                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="联系电话">
                                    <el-input v-model="formData.telephone"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="邮箱">
                                    <el-input v-model="formData.email"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="是否禁用">
                                    <el-switch v-model="isForbidden"></el-switch>
                                </el-form-item>
                            </el-col>
                        </el-row>

                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="handleEdit()">确定</el-button>
                    </div>
                </el-dialog>
            </div>

        </div>
    </div>
</div>
</body>
<!-- 引入组件库 -->
<script src="../js/vue.js"></script>
<script src="../plugins/elementui/index.js"></script>
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script src="../js/axios-0.18.0.js"></script>
<script>
    var vue = new Vue({
        el: '#app',
        data: {
            pagination: {//分页相关模型数据
                currentPage: 1,//当前页码
                pageSize: 10,//每页显示的记录数
                total: 0,//总记录数
                queryString: null//查询条件
            },
            formData: {},//表单数据
            dataList:[],
            roleList:[],    //用户角色名称列表
            roleIds:[], //用户角色id列表
            dialogFormVisible: false,//增加表单是否可见
            dialogFormVisible4Edit: false,//编辑表单是否可见
            isForbidden: false,//是否警用
            password: "",//密码
            rules: {//校验规则
                code: [{required: true, message: '用户名为必填项', trigger: 'blur'}],
                name: [{required: true, message: '项目名称为必填项', trigger: 'blur'}]
            }
        },
        //钩子函数，VUE对象初始化完成后自动执行
        created() {
            this.findPage();
        },
        methods: {
            handleSelectionChange() {
            },
            //编辑
            handleEdit() {
                console.log(this.isForbidden);
                this.formData.station = this.isForbidden;
                axios.post("/user/edituser?roleIds="+this.roleIds, this.formData).then((response) => {
                    if (response.data.flag) {
                        this.$message({
                            type: 'success',
                            message: response.data.message
                        });
                        this.findPage();
                        this.dialogFormVisible4Edit = false;
                    } else {
                        this.$message.error(response.data.message);
                    }
                }).finally(() => {
                    this.findPage();
                    this.dialogFormVisible4Edit = false;
                });
            },
            handleDelete(row) {
                this.$confirm("确认删除当前选中记录吗？", "提示", {
                    type: 'warning'
                }).then(() => {
                    axios.get("/user/deleteuser?id=" + row.id).then((res) => {
                        if (!res.data.flag) {//flag 失败时
                            this.$message.error(res.data.message);
                        } else {
                            this.$message({//flag 成功时
                                type: 'success',
                                message: "删除成功"
                            });
                            this.findPage();
                        }
                    }).catch((e) => {
                        this.showMessage(e);
                    });
                }).catch((e) => {
                });
            },
            handleAdd() {
                //表单数据校验通过，
                this.formData.station = this.isForbidden;
                axios.post("/user/adduser?roleIds=" + this.roleIds, this.formData).then((response) => {
                    if (response.data.flag) {
                        this.$message({
                            type: 'success',
                            message: response.data.message
                        });
                        this.dialogFormVisible = false;
                    } else {
                        this.$message.error(response.data.message);
                    }
                }).finally(() => {
                    this.findPage();
                });
            },

            //更改密码
            handleUpdatePassword(row) {
                let id = row.id;
                this.$prompt('请输入新密码', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消'
                    // inputPattern: /[\w!#$%&'*+/=?^_`{|}~-]+(?:\.[\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\w](?:[\w-]*[\w])?\.)+[\w](?:[\w-]*[\w])?/,
                    // inputErrorMessage: '邮箱格式不正确'
                }).then(({value}) => {

                    //点击确认时的操纵
                    axios.post(`/user/updateUser?id=${id}&password=` + value).then(response => {
                        this.$message.success("修改成功");
                    })
                }).catch(() => {

                });
            },
            //分页查询
            findPage() {
                var param = {
                    currentPage: this.pagination.currentPage,
                    pageSize: this.pagination.pageSize,
                    queryString: this.pagination.queryString
                };
                axios.post("/user/findPage", param).then((response) => {
                    this.dataList = response.data.data.rows;
                    this.pagination.total = response.data.data.total;
                }).catch((e) => {
                    this.$message.error("未知错误")
                });
            },
            // 弹出添加窗口
            handleCreate() {
                //弹出新增窗口
                this.dialogFormVisible = true;
                this.formData = {};
                //获取所有的角色信息
                axios.get(`/user/getAllRoles`).then(response => {
                    this.roleList = response.data.data;
                })
            },

            // 弹出编辑窗口
            handleUpdate(row) {
                this.dialogFormVisible4Edit = true;
                this.formData = row;
                this.roleIds = [];
                //获取所有的角色信息
                axios.get(`/user/getAllRoles`).then(response => {
                    this.roleList = response.data.data;
                });
            },
            //切换页码
            handleCurrentChange(currentPage) {
                this.pagination.currentPage = currentPage;
                //重新调用findPage方法进行分页查询
                this.findPage();
            },

        }
    })
</script>
</html>